package com.js.basicutils.permission;

// Created by JS on 2021/1/16.

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.LocationManager;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.core.content.ContextCompat;

public final class PermissionUtils {

    static AlertDialog mPermissionDialog;


    /**
     * 检查权限
     * @param context
     * @param permissions 权限集合
     * @return
     */
    public static boolean hasPermissions(Context context, String[] permissions) {
        boolean hasPer = true;
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                hasPer = false;
                break;
            }
        }
        return hasPer;
    }

    /**
     * 检查单一权限
     * @param context
     * @param permission
     * @return
     */
    public static boolean hasPermission(Context context, String permission) {
        return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 获取权限
     */
    public static void getPermission(Activity activity, String[] permissions) {
        getPermission(activity, permissions, null);
    }

    /**
     * 获取权限
     * @param activity 获取权限的页面
     * @param permissions 权限列表
     * @param listener 获取权限的回调
     */
    public static void getPermission(Activity activity, String[] permissions, PermissionListener listener) {
        PermissionActivity.setPermissionListener(listener);
        PermissionActivity.setPermissions(permissions);
        Intent intent = new Intent(activity, PermissionActivity.class);
        activity.startActivity(intent);
        activity.overridePendingTransition(0, 0); // 两个参数是进出动画的动画资源ID
    }

    /**
     * 弹出去应用设置页面设置权限的提示
     * @param activity
     * @param packageName
     */
    public static void showPermissionSettingDialog(final Activity activity, final String packageName) {
        if (mPermissionDialog == null) {
            mPermissionDialog = new AlertDialog.Builder(activity)
                    .setMessage("已禁用权限，请手动授予，然后重新进入页面")
                    .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            cancelPermissionDialog();

                            Uri packageURI = Uri.parse("package:" + packageName);
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                            activity.startActivity(intent);
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            cancelPermissionDialog();
                            activity.finish();
                        }
                    })
                    .create();
        }
        mPermissionDialog.show();
    }

    private static void cancelPermissionDialog() {
        if (mPermissionDialog != null) mPermissionDialog.cancel();
    }

    public static boolean isLocationEnable(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(false);
        String bestProvider = locationManager.getBestProvider(criteria, true);
        return !TextUtils.isEmpty(bestProvider);
    }
}
